///*
// * Copyright 2015, gRPC Authors All rights reserved.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// *     http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package keter.lab.grpc.routeguide;
//
//import com.google.common.annotations.VisibleForTesting;
//import com.google.protobuf.Message;
//import io.grpc.ManagedChannel;
//import io.grpc.ManagedChannelBuilder;
//import io.grpc.StatusRuntimeException;
//import io.grpc.examples.routeguide.*;
//import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideBlockingStub;
//import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideStub;
//
//import java.io.IOException;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Random;
//import java.util.concurrent.TimeUnit;
//import java.util.logging.Level;
//import java.util.logging.Logger;
//
///**
// * Sample client code that makes gRPC calls to the server.
// */
//public class RouteGuideClient {
//  private static final Logger logger = Logger.getLogger(RouteGuideClient.class.getName());
//
//  private final ManagedChannel channel;
//  private final RouteGuideBlockingStub blockingStub;
//  private final RouteGuideStub asyncStub;
//
//  private Random random = new Random();
//  private TestHelper testHelper;
//
//  /** Construct client for accessing RouteGuide server at {@code host:port}. */
//  public RouteGuideClient(String host, int port) {
//    this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(true));
//  }
//
//  /** Construct client for accessing RouteGuide server using the existing channel. */
//  public RouteGuideClient(ManagedChannelBuilder<?> channelBuilder) {
//    channel = channelBuilder.build();
//    blockingStub = RouteGuideGrpc.newBlockingStub(channel);
//    asyncStub = RouteGuideGrpc.newStub(channel);
//  }
//
//  public void shutdown() throws InterruptedException {
//    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
//  }
//
//  /**
//   * Blocking unary call example.  Calls getFeature and prints the response.
//   */
//  public void getFeature(int lat, int lon) {
//    info("*** GetFeature: lat={0} lon={1}", lat, lon);
//    Point request = Point.newBuilder().setLatitude(lat).setLongitude(lon).build();
//    Feature feature;
//    try {
//      feature = blockingStub.getFeature(request);
//      if (testHelper != null) {
//        testHelper.onMessage(feature);
//      }
//    } catch (StatusRuntimeException e) {
//      warning("RPC failed: {0}", e.getStatus());
//      if (testHelper != null) {
//        testHelper.onRpcError(e);
//      }
//      return;
//    }
//    if (RouteGuideUtil.exists(feature)) {
//      info("Found feature called \"{0}\" at {1}, {2}",
//          feature.getName(),
//          RouteGuideUtil.getLatitude(feature.getLocation()),
//          RouteGuideUtil.getLongitude(feature.getLocation()));
//    } else {
//      info("Found no feature at {0}, {1}",
//          RouteGuideUtil.getLatitude(feature.getLocation()),
//          RouteGuideUtil.getLongitude(feature.getLocation()));
//    }
//  }
//
//  /**
//   * Blocking server-streaming example. Calls listFeatures with a rectangle of interest. Prints each
//   * response feature as it arrives.
//   */
//  public void listFeatures(int lowLat, int lowLon, int hiLat, int hiLon) {
//    info("*** ListFeatures: lowLat={0} lowLon={1} hiLat={2} hiLon={3}", lowLat, lowLon, hiLat,
//        hiLon);
//
//    Rectangle request =
//        Rectangle.newBuilder()
//            .setLo(Point.newBuilder().setLatitude(lowLat).setLongitude(lowLon).build())
//            .setHi(Point.newBuilder().setLatitude(hiLat).setLongitude(hiLon).build()).build();
//    Iterator<Feature> features;
//    try {
//      features = blockingStub.listFeatures(request);
//      for (int i = 1; features.hasNext(); i++) {
//        Feature feature = features.next();
//        info("Result #" + i + ": {0}", feature);
//        if (testHelper != null) {
//          testHelper.onMessage(feature);
//        }
//      }
//    } catch (StatusRuntimeException e) {
//      warning("RPC failed: {0}", e.getStatus());
//      if (testHelper != null) {
//        testHelper.onRpcError(e);
//      }
//    }
//  }
//
//  /** Issues several different requests and then exits. */
//  public static void main(String[] args) throws InterruptedException {
//    List<Feature> features;
//    try {
//      features = RouteGuideUtil.parseFeatures(RouteGuideUtil.getDefaultFeaturesFile());
//    } catch (IOException ex) {
//      ex.printStackTrace();
//      return;
//    }
//
//    RouteGuideClient client = new RouteGuideClient("localhost", 8980);
//    try {
//      // Looking for a valid feature
//      client.getFeature(409146138, -746188906);
//
//      // Feature missing.
//      client.getFeature(0, 0);
//
//      // Looking for features between 40, -75 and 42, -73.
//      client.listFeatures(400000000, -750000000, 420000000, -730000000);
//
//    } finally {
//      client.shutdown();
//    }
//  }
//
//  private void info(String msg, Object... params) {
//    logger.log(Level.INFO, msg, params);
//  }
//
//  private void warning(String msg, Object... params) {
//    logger.log(Level.WARNING, msg, params);
//  }
//
//  private RouteNote newNote(String message, int lat, int lon) {
//    return RouteNote.newBuilder().setMessage(message)
//        .setLocation(Point.newBuilder().setLatitude(lat).setLongitude(lon).build()).build();
//  }
//
//  /**
//   * Only used for unit test, as we do not want to introduce randomness in unit test.
//   */
//  @VisibleForTesting
//  void setRandom(Random random) {
//    this.random = random;
//  }
//
//  /**
//   * Only used for helping unit test.
//   */
//  @VisibleForTesting
//  interface TestHelper {
//    /**
//     * Used for verify/inspect message received from server.
//     */
//    void onMessage(Message message);
//
//    /**
//     * Used for verify/inspect error received from server.
//     */
//    void onRpcError(Throwable exception);
//  }
//
//  @VisibleForTesting
//  void setTestHelper(TestHelper testHelper) {
//    this.testHelper = testHelper;
//  }
//}
